//
//  AustraliaViewController.m
//  littleLight's Project-A
//
//  Created by dllo on 16/5/21.
//  Copyright © 2016年 light. All rights reserved.
//

#import "AustraliaViewController.h"
#import "FormulateTableViewCell.h"
#import <AFNetworking.h>
#import "FormulateBaseModel.h"
#import "ImageWebViewController.h"
#import "SmallImageViewController.h"
@interface AustraliaViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *allTableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation AustraliaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    
    self.allTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width, self.view.frame.size.height - 40 - 64 - 49) style:UITableViewStylePlain];
    self.allTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.allTableView];
    self.allTableView.delegate = self;
    self.allTableView.dataSource = self;
    
    [self.allTableView registerClass:[FormulateTableViewCell class] forCellReuseIdentifier:@"reuse"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
    [manager GET:@"http://web.breadtrip.com/product/topics/more/?start=0&count=10&country=3735" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //NSLog(@"%@",responseObject);
        NSDictionary *dataDic = [responseObject objectForKey:@"data"];
        NSArray *array = [dataDic objectForKey:@"topic_list"];
        for (NSDictionary *dic in array) {
            FormulateBaseModel *model = [[FormulateBaseModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
            
        }
        [self.allTableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // NSLog(@"%@", error);
    }];

}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    FormulateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    FormulateBaseModel *model = [self.dataArray objectAtIndex:indexPath.row];
    
    cell.array = model.products;
    cell.model = model;
    __block AustraliaViewController *VC = self;
    cell.myBlock = ^(FormulateBaseModel *model){
        ImageWebViewController *imageWebVC = [[ImageWebViewController alloc] init];
        UINavigationController *NaVC = [[UINavigationController alloc] initWithRootViewController:imageWebVC];
        imageWebVC.webString = model.url;
        imageWebVC.titleString = model.title;
        [VC presentViewController:NaVC animated:YES completion:^{
            
            
        }];
        
        
    };
    cell.youBlock = ^(FormulateListBateModel *model){
        SmallImageViewController *smallVC = [[SmallImageViewController alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:smallVC];
        smallVC.webString = model.url;
        smallVC.titleString = model.text;
        [VC presentViewController:naVC animated:YES completion:^{
            
            
        }];
        
        
        
        
    };


    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 500;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
